﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game1942.Library.View
{
    public class LifeView : IObserver<Algo42>, IView
    {
        private Texture2D lifeTexture;
        private SpriteFont font;
        private int lifes = 3;
        private int xPos, yPos;
        private IDisposable unsubscriber;

        public LifeView(int xPos, int yPos)
        {
            this.xPos = xPos;
            this.yPos = yPos;
        }

        public void LoadContent(Texture2D lifeTexture, SpriteFont font)
        {
            this.lifeTexture = lifeTexture;
            this.font = font;
        }

        public void Draw(SpriteBatch sprite)
        {
            sprite.Draw(lifeTexture,
                        new Rectangle(xPos,yPos,50,50), Color.White);

            sprite.DrawString(font,
                "PLAYER 1",
                new Vector2(xPos + 55, yPos ),
                Color.White);

            sprite.DrawString(font,
                "LIFES  "+ "x  " + lifes.ToString(),
                new Vector2(xPos + 55, yPos + 25),
                Color.White);

            sprite.DrawString(font, "PLAYER 2", new Vector2(xPos + 600, yPos),
                Color.White);

            sprite.DrawString(font,
               "INSERT COINS" ,
               new Vector2(xPos + 585, yPos + 25),
               Color.White);
        }

        public bool Visible()
        {
            throw new NotImplementedException();
        }

        #region [Observer Methods]

        public void Subscribe(IObservable<Algo42> provider)
        {
            if (provider != null)
                unsubscriber = provider.Subscribe(this);
        }

        //Provides the observer with new data.
        public void OnNext(Algo42 value)
        {
            lifes = value.Lifes;
        }

        //Notifies the observer that the provider has experienced an error condition.
        public void OnError(Exception error)
        {
            //Do nothing
        }

        //Notifies the observer that the provider has finished sending push-based notifications.
        public void OnCompleted()
        {
            unsubscriber.Dispose();
        }

        #endregion
    }
}
